$(function () {
    initEvent();
    getUserInfo();
});

function initEvent() {
    $("#submitMsg").off('click').on('click', function () {
        submitMsg();
    });
}

// 获取居住地(城市联动)
function getCity(flag,id,objid){
    if(objid == 'icity' || objid == 'iarea' || objid == 'wcity' || objid == 'warea'){
        if('' == id){
            falert('请选择正确的省市区');
            return false;
        }
    }
    id = id || '';
    flag = flag || false;
    var datas = {
        interface:'users@getCity',    
    };
    if(flag && id){
        datas.id = id;
    }
    common.ajax({
        data: datas,
        unlock: 1,   
        success: function (data) {
            if (data.status == '2000') {
                if(flag == 'city'){
                    var html = '<option value="" val="">选择市</option>';
                    $('#'+objid).html(html);
                    if(objid == 'wcity'){
                        $('#warea').html('<option value="" val="">选择区</option>');
                    }else if(objid == 'icity'){
                        $('#iarea').html('<option value="" val="">选择区</option>');
                    }
                    html += common.formatTemplate(data.result,'<option value="{city_name}" val="{city_key}">{city_name}</option>',true);
                    $('#'+objid).html(html);
                }else if(flag == 'area'){
                    var html = '<option value="" val="">选择区</option>';
                    html += common.formatTemplate(data.result,'<option value="{city_name}" val="{city_key}">{city_name}</option>',true);  
                    $('#'+objid).html(html);
                }else{
                    var html = '<option value="" val="">选择省</option>';
                    html += common.formatTemplate(data.result,'<option value="{city_name}" val="{city_key}">{city_name}</option>',true);  
                    $('#'+objid).html(html);
                }
             
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });  
}

function getFldInfo(){
    var value = $('#parent').find("option:selected").text()+'->'+$('#child1').find("option:selected").text()+'->'+$('#child2').find("option:selected").text()+'->'+$('#child3').find("option:selected").text();
    $('#uw_fld').val($('#child3').find("option:selected").val());
    $('#uw_fld_name').val(value);
}

var list2 = {'I':['64','65'],'J':['66','69'],'K':['70','71'],'L':['72'],'M':['74'],'O':['79','81'],'P':['82']};
var list2_ = {'64':'互联网和相关服务','65':'软件和信息技术服务业','66':'货币金融服务','69':'其他金融业','70':'房地产业','71':'租赁业','72':'商务服务业','74':'专业技术服务业','79':'居民服务业','81':'其他服务业','82':'教育'};

// 第3级
var list3 = {'64':['642','649'],'65':['659','651'],'66':['663'],'69':['691','693'],'70':['703'],'71':['712'],'72':['723','727','729'],'74':['749'],'79':['791','799'],'81':['819'],'82':['829']};
var list3_ = {'642':'互联网信息服务','649':'其他互联网服务','651':'软件开发','659':'其他信息技术服务业','663':'非货币银行服务','691':'金融信托与管理服务','693':'非金融机构支付服务','703':'房地产中介服务','712':'文化及日用品出租','723':'咨询与调查','727':'旅行社及相关服务','729':'其他商务服务业','749':'其他专业技术服务业','791':'家庭服务','799':'其他居民服务业','819':'其他未列明服务业','829':'技能培训、教育辅助及其他教育'};

//第4级
var list4 = {'642':['6420'],'649':['6490'],'651':['6510'],'659':['6599'],'663':['6631','6633'],'691':['6910'],'693':['6930'],'703':['7030'],'712':['7129'],'723':['7239'],'727':['7279'],'729':['7299'],'749':['7491','7499'],'791':['7910'],'799':['7990'],'819':['8190'],'829':['8299']};
var list4_ = {'6420':'互联网信息服务','6490':'其他互联网服务','6510':'软件开发','6599':'其他未列明信息技术服务业','6631':'金融租赁服务','6633':'典当','6910':'金融信托与管理服务','6930':'非金融机构支付服务','7030':'房地产中介服务','7129':'其他文化及日用品出租','7239':'其他专业咨询','7279':'其他旅行社相关服务','7299':'其他未列明商务服务业','7491':'专业化设计服务','7499':'其他未列明专业技术服务业','7910':'家庭服务','7990':'其他居民服务业','8190':'其他未列明服务业','8299':'其他未列明教育'};

var default_ = '<option value="">请选择</option>';

function clickOpt(num){
    var html1=default_;
     $.each(list2[num],function(key,val){
        html1 += '<option value="'+val+'">'+list2_[val]+'</option>';
         $('#child1').html(html1);
      });
     $('#child2').html(default_);
     $('#child3').html(default_);
     $('#child4').html(default_);
   
}

function clickOpt1(num,v,list){
    var html2=default_;
     var arr = list3_;
     if(3==num){
       arr = list4_;
     }
     $.each(list,function(key3,val3){
        html2 += '<option value="'+val3+'">'+arr[val3]+'</option>';
         $('#child'+num).html(html2);
      });
   
}

// 提交个人信息
function msgPeople() {
    var datas = common.formatFormData('msgInfo');
    if(!checkParams(datas)) return false;
    
    var email = $('#u_email').val();
    
    var result = common.checkForm().checkEmail(email, function (result, error) {
        if ('' == $.trim(email) || $.trim(email) == $("#u_email").attr('placeholder')) {
            falert('请输入您的邮箱');
            return false;
        } else if (result == false) {
            falert(error);
            return false;
        }
    }).value;
    
    if(result == true){
        datas.interface = 'users@douserInfo';      
        common.ajax({
            data: datas,
            success: function (data) {
                if (data.status == '2000') {
                    getTplHtml('msg',$('#msgInfo'));
                    $(".banner1").css("min-height","9.0rem");
                    common.ajax({
                        data: {interface:'users@userInfo'},
                        success: function (data) {
                            if (data.status == '2000') {
                                setTimeout(function(){$('#msgInfo').html(common.formatTemplate(data.result,$('#msgInfo').html()));}, 500);
                            } else if (data.status == '5000') {
                                falert(data.msg);
                            }
                        }
                    });
                    falert('保存成功');
                    $('#checkSubmit').val(parseInt($('#checkSubmit').val())+1);
                } else if (data.status == '5000') {
                    falert(data.msg);
                }
            }
        });
    }
    
}
var flag=true;

// 提交联系人信息
function contactMsg(){
    var contactInfo1 = common.formatFormData('contactInfo1');
    if(!checkParams(contactInfo1)) return false;
    var contactInfo2 = common.formatFormData('contactInfo2');
    if(!checkParams(contactInfo2)) return false;
    
    var relationArr = [];
    var phonearr = [];
    var flag = true;
    var nflag = true;
    for (var i = 1; i <= 2; i++) {
        var relation =  $("#relation"+i).val();
        var phone = $("#phone"+i).val();
        var name = $("#name" + i).val();
        common.checkForm().checkName(name, function (result, error) {
            if (result == false) {
                falert(error);
                nflag = false;
                return false;
            }else{
                nflag = true;
            }
        });
        common.checkForm().checkMobile(phone, function (result, error) {
            if (result == false) {
                falert(error);
                flag = false;
                return false;
            }else{
                flag = true;
            }
        });
        if(phone){
            phonearr.push(phone);
        }
        if(relation=='父亲' || relation=='母亲' || relation=='配偶'){
            relationArr.push(relation);
        }
    }
    if(!flag || !nflag){
        return false;
    }
    if(/(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+relationArr.join("\x0f\x0f") +"\x0f")){
        falert('两个紧急联系人不能重复');
        return false;
    }
    if(/(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+phonearr.join("\x0f\x0f") +"\x0f")){
        falert('手机号不能重复');
        return false;
    }

    var arr = [];
    arr.push(contactInfo1);
    arr.push(contactInfo2);
    var datas = new Object;
    datas.relations = arr;
    datas.interface = 'users@doContact'; 
    
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                $('#contactMsg').remove();
                getTplHtml('contact1',$('#contactInfo1'));
                getTplHtml('contact2',$('#contactInfo2'));
                $(".banner3").css("min-height","10.8rem");
                common.ajax({
                    data: {interface:'users@contact'},
                    success: function (data) {
                        if (data.status == '2000') {
                            setTimeout(function(){
                                $('#contactInfo1').html(common.formatTemplate(data.result[0],$('#contactInfo1').html()));
                                $('#contactInfo2').html(common.formatTemplate(data.result[1],$('#contactInfo2').html()));
                            }, 500);
                        } else if (data.status == '5000') {
                            falert(data.msg);
                        }
                    }
                });
                falert('保存成功');
                $('#checkSubmit').val(parseInt($('#checkSubmit').val())+1);
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}

// 提交工作信息
function workmsgSave() {
    var datas = common.formatFormData('workInfo');
    if(!checkParams(datas)) return false;
    var biaos = $('#biaos').val();
    datas.interface = 'users@doworkInfo';     
    
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                getTplHtml('work'+biaos,$('#workInfo'));
                if(biaos == '1'){
                    $(".banner2").css("min-height","12.3rem");
                }else if(biaos == '2'){
                    $(".banner2").css("min-height","6.6rem");
                }else if(biaos == '3'){
                    $(".banner2").css("min-height","6.6rem");
                }
                common.ajax({
                    data: {interface:'users@workInfo'},
                    success: function (data) {
                        if (data.status == '2000') {
                            if(data.result.cus_flag == '1'){
                                data.result.cus_flag = '自雇';
                            }else if(data.result.cus_flag == '2'){
                                data.result.cus_flag = '在职';
                            }else if(data.result.cus_flag == '3'){
                                data.result.cus_flag = '学生';
                            }
                            setTimeout(function(){$('#workInfo').html(common.formatTemplate(data.result,$('#workInfo').html()));}, 500);
                        } else if (data.status == '5000') {
                            falert(data.msg);
                        }
                    }
                });
                falert('保存成功');
                $('#checkSubmit').val(parseInt($('#checkSubmit').val())+1);
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}

function getUserInfo(){
    var datas = {
        interface:'users@backUserInfo',
    };
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                $('#msgInfo').html(common.formatTemplate(data.result.userInfo,$('#msgInfo').html(),true));
                $('#weed').find('option[value="'+data.result.userInfo.u_marriage+'"]').prop('selected',true);
                $('#education').find('option[value="'+data.result.userInfo.u_edu_degree+'"]').prop('selected',true);
                setTimeout(function(){getCity(false,'','iprovince')}, 100);
                setTimeout(function(){$('#iprovince').find('option[value="'+data.result.userInfo.u_province+'"]').prop('selected',true)}, 300);
                setTimeout(function(){getCity('city',$('#iprovince').find('option:selected').attr('val'),'icity')}, 500);
                setTimeout(function(){$('#icity').find('option[value="'+data.result.userInfo.u_city+'"]').prop('selected',true)}, 700);
                setTimeout(function(){getCity('area',$('#icity').find('option:selected').attr('val'),'iarea')}, 900);
                setTimeout(function(){$('#iarea').find('option[value="'+data.result.userInfo.u_area+'"]').prop('selected',true)}, 1100);
				
                if(data.result.workInfo.cus_flag == '1'){
                    data.result.workInfo.cus_flag = '自雇';
                    $(".banner2").css("min-height","17rem");
                    getTplHtml('workform',$('#workform'));
                    setTimeout(function(){$('#workInfo').html(common.formatTemplate(data.result.workInfo,$('#workInfo').html(),true))}, 200);
                    $('#biaos').val('1');
                    setTimeout(function(){
                        setTimeout(function(){getCity(false,'','wprovince')}, 100);
                        setTimeout(function(){$('#wprovince').find('option[value="'+data.result.workInfo.uw_province+'"]').prop('selected',true)}, 300);
                        setTimeout(function(){getCity('city',$('#wprovince').find('option:selected').attr('val'),'wcity')}, 500);
                        setTimeout(function(){$('#wcity').find('option[value="'+data.result.workInfo.uw_city+'"]').prop('selected',true)}, 700);
                        setTimeout(function(){getCity('area',$('#wcity').find('option:selected').attr('val'),'warea')}, 900);
                        setTimeout(function(){$('#warea').find('option[value="'+data.result.workInfo.uw_area+'"]').prop('selected',true)}, 1100);

                        var strArr = data.result.workInfo.uw_fld_name.split('->');
                        setTimeout(function(){$('#parent').find('option:contains("'+strArr[0]+'")').prop('selected',true)}, 50);
                        setTimeout(function(){clickOpt($('#parent').val())}, 100);
                        setTimeout(function(){$('#child1').find('option:contains("'+strArr[1]+'")').prop('selected',true)}, 150);
                        setTimeout(function(){clickOpt1(2,$('#child1').val(),list3[$('#child1').val()])}, 200);
                        setTimeout(function(){$('#child2').find('option:contains("'+strArr[2]+'")').prop('selected',true)}, 250);
                        setTimeout(function(){clickOpt1(3,$('#child2').val(),list4[$('#child2').val()])}, 300);
                        setTimeout(function(){$('#child3').find('option:contains("'+strArr[3]+'")').prop('selected',true)}, 350);
                    }, 200);
                }else if(data.result.workInfo.cus_flag == '2'){
                    data.result.workInfo.cus_flag = '在职';
                    $('#workInfo').html(common.formatTemplate(data.result.workInfo,$('#workInfo').html(),true));
                    setTimeout(function(){
                        $('#biaos').val('2');

                        setTimeout(function(){getCity(false,'','wprovince')}, 100);
                        setTimeout(function(){$('#wprovince').find('option[value="'+data.result.workInfo.uw_province+'"]').prop('selected',true)}, 300);
                        setTimeout(function(){getCity('city',$('#wprovince').find('option:selected').attr('val'),'wcity')}, 500);
                        setTimeout(function(){$('#wcity').find('option[value="'+data.result.workInfo.uw_city+'"]').prop('selected',true)}, 700);
                        setTimeout(function(){getCity('area',$('#wcity').find('option:selected').attr('val'),'warea')}, 900);
                        setTimeout(function(){$('#warea').find('option[value="'+data.result.workInfo.uw_area+'"]').prop('selected',true)}, 1100);
                    }, 200);
                }else if(data.result.workInfo.cus_flag == '3'){
                    $(".banner2").css("min-height","8.5rem");
                    data.result.workInfo.cus_flag = '学生';
                    getTplHtml('schoolform',$('#workform'));
                    setTimeout(function(){$('#workInfo').html(common.formatTemplate(data.result.workInfo,$('#workInfo').html(),true))}, 200);
                    setTimeout(function(){
                        laydate.render({elem: '#school_date',showBottom: false,min: '2010-1-1',max: 0,trigger: 'click'});
                        $('#com_years').find('option[value="'+data.result.workInfo.com_years+'"]').prop('selected',true)
                    }, 200);
                    $('#biaos').val('3');
                }
                
                $('#contactInfo1').html(common.formatTemplate(data.result.contact[0],$('#contactInfo1').html(),true));
                setTimeout(function(){$('#relation1').find('option[value="'+data.result.contact[0].ur_relation+'"]').prop('selected',true);}, 100);
                $('#contactInfo2').html(common.formatTemplate(data.result.contact[1],$('#contactInfo2').html(),true));
                setTimeout(function(){$('#relation2').find('option[value="'+data.result.contact[1].ur_relation+'"]').prop('selected',true);}, 100);

                $('#userInfo').html(common.formatTemplate(data.result.userInfo,$('#userInfo').html()));
                $('#bankInfo').html(common.formatTemplate(data.result.userInfo,$('#bankInfo').html()));
                
                $('#backorder').show();
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });  
}

// 提交信息
function submitMsg() {
    if($('#checkSubmit').val() <= '0'){
        falert('请您先修改信息并保存');
        return false;
    }
    var datas = {
        interface:'order@saveOrder', 
        b_id:common.get('id'),
    };     
    
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                falert('提交信息成功','/order/orderlist');
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}
